package com.bosssoft.hr.train.j2se.colletion;
import com.bosssoft.hr.train.j2se.pojo.User;
import lombok.extern.slf4j.Slf4j;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * @Classname: HashSetDemo
 * @Description:此类用于其中有User的数值完全一样要求能被过滤
 * @author: zoubinyan
 * @Date: 2023/2/22
 */

@Slf4j
public class HashSetDemo extends BaseCollectionDemo {
    /**
     * 定义userHashSet
     */
    private Set<User> userHashSet = new HashSet<>();

    public void visitByIterator() {
        //调用父类初始化并构造
        User[] users = generateData();
        for(User user: users){
            userHashSet.add(user);
        }
        /**
         * 构造的值是判断唯一的3个字段，即id,name,code
         */
        User user = new User(
                new Long(1),
                String.format("BS%03d",1),
                "同学"+1
        );
        /**
         *用log而不是prinltin
         */
        // userHashSet中没有重复的对象，通过User中的hashcode和equal方法判断是否重复
        userHashSet.add(user);
        log.info("用户数量%d",userHashSet.size());
        //通过迭代器输出
        Iterator<User> userIterator=userHashSet.iterator();
        while (userIterator.hasNext()){
            log.info("当前用户:",userIterator.next());
        }
    }
}